package com.scgk.sync.common.exception;


import com.scgk.sync.common.BaseResponse;
import com.scgk.sync.common.ErrorType;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.internal.engine.path.PathImpl;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.core.annotation.Order;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * description: BaseResponse
 * date: 2020-09-02 08:15
 * @author: chenxd
 * version: 1.0
 */
@RestControllerAdvice(annotations = {RestController.class, Controller.class})
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(BusinessException.class)
    public BaseResponse businessHandler(BusinessException e) {
        log.error(e.getMessage(), e);
        return BaseResponse.error(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(ParamInvalidException.class)
    public BaseResponse paramInvalidHandler(ParamInvalidException e) {
        log.error(e.getMessage(), e);
        return BaseResponse.error(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public BaseResponse seriousHandler(Exception e) {
        log.error(e.getMessage(), e);
        return BaseResponse.error();
    }
}
